// 引入
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";

// 注册
Vue.use(Vuex)

// 默认导出
export default new Vuex.Store({
  plugins: [createPersistedState({
    key: "taskList",
    paths: ["taskList", "flag"]
  })],
  state: {
    taskList: [
      { id: 100, text: "吃饭", isSelected: true },
      { id: 101, text: "睡觉", isSelected: false },
      { id: 102, text: "打豆豆", isSelected: true },
    ],  // 任务列表
    flag: 0
  },
  mutations: {
    // 修改任务列表
    setTaskList (state, taskList) { 
      state.taskList = taskList
    },
    setFlag (state, flag) { 
      state.flag = flag
    },
  },
  getters: {
    showList (state) { 
      console.log(state);
      if (state.taskList.length) {
        if (state.flag === 0) {
          return state.taskList
        } else if (state.flag === 1) {
          return state.taskList.filter(item => item.isSelected === true)
        } else {
          return state.taskList.filter(item => item.isSelected === false)
        }
      } else { 
        return []
      }
      
    }
  }
})
